package com.xyyl.patient.core.support;

import java.io.Serializable;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xyyl.patient.core.util.PageData;

/**
 * @projectName:xyyl_patient
 * @ClassName: Pager
 * @Description: 分页信息封装
 * @author chen
 * @date 2017年11月24日
 *
 */
public class Pager implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 页编号 : 第几页
	 */
	protected int pageNo = 1;
	/**
	 * 页大小 : 每页的数量
	 */
	protected int pageSize = 15;
	
	/**
	 * 偏移量 : 第一条数据在表中的位置
	 */
	@JSONField(serialize=false)
	protected int offset;
	
	/**
	 * 限定数 : 每页的数量
	 */
	@JSONField(serialize=false)
	protected int limit;

	/** 封装排序条件 **/
	@JSONField(serialize=false)
	protected String orderBy;

	/** 默认倒序 **/
	@JSONField(serialize=false)
	private String sort = "desc";

	/**
	 * 查询结果
	 */
	protected List<?> data;

	/**
	 * 总条数
	 */
	protected int totalCount;

	/**
	 * 总页数
	 */
	protected int totalPages;

	/** 查询条件封装 **/
	@JSONField(serialize=false)
	private PageData pageData;
	
	/**分页是否失效 **/
	@JSONField(serialize=false)
	private boolean invalid;
	
	/** 是否移除select count(0) sql中的order by **/
	@JSONField(serialize=false)
	private boolean isHasOrderBy=true;
	
	/** 是否移除select count(0) sql中的group by **/
	@JSONField(serialize=false)
	private boolean isHasGroupBy=true;
	
	
	public boolean isHasOrderBy() {
		return isHasOrderBy;
	}

	public void setHasOrderBy(boolean isHasOrderBy) {
		this.isHasOrderBy = isHasOrderBy;
	}

	public boolean isHasGroupBy() {
		return isHasGroupBy;
	}

	public void setHasGroupBy(boolean isHasGroupBy) {
		this.isHasGroupBy = isHasGroupBy;
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public Pager() {
		this.calcOffset();
		this.calcLimit();
	}

	public Pager(int pageNo, int pageSize) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.calcOffset();
		this.calcLimit();
	}

	/**
	 * 计算偏移量
	 */
	private void calcOffset() {
		this.offset = (pageNo - 1) * pageSize;
	}

	/**
	 * 计算限定数
	 */
	private void calcLimit() {
		this.limit = pageSize;
	}

	/**
	 * 获得当前页的页号,序号从1开始,默认为1.
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 获得每页的记录数量,默认为1.
	 */
	public int getPageSize() {
		return pageSize;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	/**
	 * 根据pageNo和pageSize计算当前页第一条记录在总结果集中的位置,序号从0开始.
	 */
	public int getOffset() {
		return (pageNo - 1) * pageSize;
	}

	public int getLimit() {
		return limit;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	// -- 访问查询结果函数 --//

	/**
	 * 取得总记录数, 默认值为-1.
	 */
	public int getTotalCount() {
		return totalCount;
	}

	public List<?> getData() {
		return data;
	}

	public void setData(List<?> data) {
		this.data = data;
	}

	/**
	 * 设置总记录数.
	 */
	public void setTotalCount(final int totalCount) {
		this.totalCount = totalCount;
		this.totalPages = this.getTotalPages();
	}

	/**
	 * 根据pageSize与totalCount计算总页数, 默认值为-1.
	 */
	public int getTotalPages() {
		if (totalCount < 0) {
			return -1;
		}
		int pages = totalCount / pageSize;
		return totalCount % pageSize > 0 ? ++pages : pages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public boolean isInvalid() {
		return invalid;
	}

	public void setInvalid(boolean invalid) {
		this.invalid = invalid;
	}

    
}
